# 作者: 陈其志 
# 2025年02月19日10时30分55秒 
# 2716847110@qq.com
def use_set():
    my_set = {'apple', 'banana', 'apple'}
    print(my_set)

    my_set_1 = {}  # 是一个集合
    print(type(my_set_1))

    my_set_2 = set()  # 空集合
    print(type(my_set_2))

    # 用in判断一个元素是不是在集合里面
    print('apple' in my_set)


def use_copy():
    a = [1, 2, 3]
    b = [1, 2, 3]
    print(id(a))
    print(id(b))
    a[0] = 5
    print(b)
    print(id(a))
    print(id(b))


def set_add():
    fruits = {"apple", "banana", "cherry"}
    fruits.add('tomato')
    print(fruits)


def set_clear():
    fruits = {"apple", "banana", "cherry"}
    fruits.clear()
    print(fruits)


def set_copy():
    fruits = {"apple", "banana", "cherry"}
    print(id(fruits))
    set_a = fruits.copy()
    print(set_a)
    print(id(set_a))  # copy是在数据区创建一个新的内存地址来存取新的数据，所以copy会改变内存地址


def set_difference():
    """
    返回多个集合的差集
    :return:
    """
    x = {"apple", "banana", "cherry"}
    y = {"google", "microsoft", "apple"}
    print(x.difference(y))  # x集合有而y集合没有的,返回给x
    print(y.difference(x))


def set_discard():
    fruits = {"apple", "banana", "cherry"}
    fruits.clear()
    print(fruits)


def set_intersection():
    """
    返回几个集合的交集
    :return:
    """
    x = {"a", "b", "c"}
    y = {"c", "d", "e"}
    z = {"f", "g", "c"}
    result = x.intersection(y, z)
    print(result)


def set_remove():
    """
    删除集合中的某个元素
    :return:
    """
    fruits = {"apple", "banana", "cherry"}
    fruits.remove('apple')
    print(fruits)


def set_union():
    """
    返回两个集合的并集
    :return:
    """
    x = {"apple", "banana", "cherry"}
    y = {"google", "runoob", "apple"}
    x.union(y)
    print(x)


def set_update():
    """
    给集合添加集合
    :return:
    """
    x = {"apple", "banana", "cherry"}
    y = {"google", "runoob", "apple"}
    x.update(y)
    print(x)


def use_set_sys():
    """
    练习集合的快捷操作
    :return:
    """
    a = set('abcdef')
    b = set('abcdez')
    print(a, b)
    print(a - b)  # 打印a有而b没有的
    print(a | b)  # 打印ab的并集
    print(a & b)  # 打印ab的交集
    print(a ^ b)  # 打印并集 - 交集


if __name__ == '__main__':
    # set_add()
    # set_clear()
    # set_copy()
    # set_difference()
    # set_clear()
    # set_intersection()
    # set_remove()
    # set_union()
    # set_update()
    # use_set()
    use_set_sys()
